import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.lang.model.element.NestingKind;
import javax.print.Doc;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JiangsuTiqu {
/**
 * 手动添加一个日期   文件名：2015_中国保监会江苏监管局行政处罚决定书〔2009〕9号_2009-03-10.html
 *
 *
 * */

    public static void main(String[] args) throws IOException {
//        System.out.println("test");
        MongoClient client = new MongoClient("192.168.1.173",27017);
        MongoCollection<org.bson.Document> target = client.getDatabase("CCBD").getCollection("监管处罚_江苏监管局_TEST");

        File[] f_all = new File("D:\\所有程序\\程存淦\\Java\\银行\\江苏监管局\\非表格").listFiles();
        int count = 0;
        for (File f:f_all) {
            List<String> list = GetFileStr(f);

            if (f.getName().equals("1308_中国保监会江苏监管局行政处罚决定书〔2016〕71号_2016-10-26.html")){
                int i = 0;
            }

            Document html = Jsoup.parse(f, "GBK", "http://example.com/");
            org.bson.Document document = new org.bson.Document("_id",f.getName());
            if(list.size() >0){
                document.append("文号",GetWenHao(html, f));
      //          document.append("作出处罚决定的日期",list.get(list.size()-1));
                document.append("作出处罚决定的日期",GetDate(html));
                document.append("机关名称",GetName(f.getName()));
//                GetName(f.getName());
                document.append("处罚对象", GetChuFaDuiXiang(list, html));
                document.append("行政处罚决定", GetJueding(list, html));
                document.append("案由", GetAnyou(list, html));
                document.append("行政处罚依据", GetBasis(list));

                //System.out.println(document);

                count++;
                target.updateOne(new BasicDBObject("_id", document.getString("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
                System.out.println(document.get("_id"));
            }

        }

        System.out.println("共计： " + count);

        System.out.println("---------------------------------");
        ToExcel.writeToExcel();
    }

    //文号
    static String GetWenHao(Document html, File file) {
        String html_text = html.text();

        Pattern pattern = Pattern.compile(".*?保监.*?号{1}");
        Matcher matcher = pattern.matcher(html_text);
        if (matcher.find()){
            String str = matcher.group();
            int start = 0;
            if (str.contains("处罚决定书")){
                start = str.indexOf("处罚决定书")+5;
            }
            int index = str.indexOf("号")+1;
            start = start > index ? 0 : start;
            return str.substring(start, index);
        }



        pattern = Pattern.compile("（.*?保监.*?号{1}");
        matcher = pattern.matcher(file.getName());
        if (matcher.find()){
            String str = matcher.group();
            int index = str.indexOf("号")+1;
            return str.substring(1, index);
        }

        pattern = Pattern.compile("\\(.*?监.*?号{1}");
        matcher = pattern.matcher(file.getName());
        if (matcher.find()){
            String str = matcher.group();
            int index = str.indexOf("号")+1;
            return str.substring(0, index);
        }

        return "";
    }


//    行政处罚决定
    static String GetJueding( List<String> list, Document html){

        StringBuffer sb = new StringBuffer();
//        for (String s: list) {
//            s = s.replace("分局","局");
//            if(s.contains("我局决定")&&!s.endsWith("：")){
//                s = s.substring(s.indexOf("我局决定")+4);
////                System.out.println(s);
////                return s;
//                sb.append(s);
//            }
//        }
//        if (sb.length() != 0)
//            return sb.toString();

        String html_text = html.text();

        Pattern pattern = Pattern.compile("(警告).*?罚款");
        Matcher matcher = pattern.matcher(html_text);
        while (matcher.find()){
            sb.append(matcher.group()+"; ");
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局责令.*?罚款");
        matcher = pattern.matcher(html_text);
        while (matcher.find()){
            String str = matcher.group();
            str = str.substring(str.indexOf("局")+1)+";";
            sb.append(str);
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局作出.*?如下(行政)?处罚.*?罚款");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("对")+1)+";";
            sb.append(str);
        }
        if (sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("作出.*行政处罚");
        matcher = pattern.matcher(html_text);
        while (matcher.find()) {
            String str = matcher.group();
            str = str.substring(str.indexOf("出")+1)+";";
            sb.append(str);
        }
        if(sb.length() != 0)
            return sb.toString();

        pattern = Pattern.compile("我分?局决定.*的(行政)?处罚");
        matcher = pattern.matcher(html_text);
        while (matcher.find()){
            String str = matcher.group();
            str = str.substring(str.indexOf("定"), str.length()-2);
            sb.append(str);
        }
        return sb.toString();
    }


    //作出处罚决定的机关名称
    static String GetName(String fName){
 //       System.out.println(fName);
        String[] s_all = fName.split("_");
        if(s_all.length >0 && s_all[1].contains("行政处罚决定书")){
//            System.out.println(s_all[1].substring(0,s_all[1].indexOf("行政处罚决定书")));
            return s_all[1].substring(0,s_all[1].indexOf("行政处罚决定书"));
        }
        return null;
    }
    //主要违法违规事实（案由）
    static String GetAnyou(List<String> list, Document doc){
        StringBuffer sb = new StringBuffer();
        boolean add = false;
        for (String s:list) {
            if(s.startsWith("综上") || s.contains("上述行为")){
                break;
            }
            if(add){
                sb.append(s);
            }
            if(s.startsWith("经查"))
                add = true;

        }

        if (sb.toString().startsWith("上述行为") || sb.toString().startsWith("该公司上述行为") || sb.toString().startsWith("你公司上述行为"))
            sb.delete(0, sb.length());

        if (sb.length() == 0) {
            add = false;
            for (String s : list) {
                if (s.contains("上述") && add){
                    sb.append(s);
                    break;
                }
                if (add){
                    sb.append(s);
                }
                if (s.endsWith("审理终结。"))
                    add = true;
            }
        }


        if (sb.length() == 0) {
            for (String s : list){
                if (s.startsWith("经查")){
                    sb.append(s);
                }
            }
        }

        if (sb.length() == 0){
            String html_text = doc.text();
            String pattern = "(经查.*[\\s\\S]*综上)|(经查.*[\\s\\S]*上述)|(题：.*[\\s\\S]*上述)";
            Pattern re = Pattern.compile(pattern);
            Matcher matcher = re.matcher(html_text);
            while (matcher.find()){
                String str = matcher.group();
                sb.append(str.substring(2, str.length() - 2));
            }
        }

        if (sb.length() == 0){
            String html_text = doc.text();
            String pattern = "为：.*[\\s\\S]*上述行为";
            Pattern re = Pattern.compile(pattern);
            Matcher matcher = re.matcher(html_text);
            while (matcher.find()){
                String str = matcher.group();
                sb.append(str.substring(2, str.length() - 4));
            }
        }

        return sb.toString();
    }
    static List<String> GetFileStr(File input) throws IOException {
        List<String> list = new ArrayList<>();
        Document doc = Jsoup.parse(input, "GBK", "http://example.com/");

        Elements ps = doc.select("p"); //带有href属性的a元素
        for (Element p:ps) {
            if(p.text().trim().replace(" ","").length() >0){
                //System.out.println(p.text()); //输出文档
                list.add(p.text().trim().replace(" ",""));
            }
        }
        return list;
    }
//    行政处罚依据
    static String GetBasis(List<String> list) throws IOException {
        List<String> basis_info = new ArrayList<>();

        String pattern = "违反了.*?条?项?(的)?规定";
        Pattern re = Pattern.compile(pattern);

        for (String i : list) {
            Matcher matcher = re.matcher(i);
            while (matcher.find()){
                String s = matcher.group();
                basis_info.add(s.substring(3, s.length()-3)); //去除开头的  违反了   结尾的   的规定
            }
        }

        // [《保险公司管]
        if(basis_info.toString().equals("[《保险公司管]")){
            pattern = "违反了.*?条?项?的规定";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()){
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length()-3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[《保险专业代理机构监]
        if(basis_info.toString().equals("[《保险专业代理机构监]")){
            pattern = "(违反了.*?的规定)|(违反了.*?条)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()){
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length()-3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[]
        if (basis_info.toString().equals("[]")){
            pattern = "(构成了.*?条?项?的违规)|(依据.*?条?项?的规定)|(根据.*?条?项?的规定)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()){
                    String s = matcher.group();
                    basis_info.add(s.substring(3, s.length()-3)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }

        //[]
        if (basis_info.toString().equals("[]")){
            pattern = "(违反.*条)|(根据.*条)";
            re = Pattern.compile(pattern);
            for (String i : list) {
                Matcher matcher = re.matcher(i);
                while (matcher.find()){
                    String s = matcher.group();
                    basis_info.add(s.substring(2)); //去除开头的  违反了   结尾的   的规定
                }
            }
        }


        return basis_info.toString();
    }

    //处罚日期
    static String GetDate(Document html) {
       String html_text = html.text();

       Pattern pattern = Pattern.compile(".{4}年.{1,3}月.{1,5}日");
       Matcher matcher = pattern.matcher(html_text);

       List<String> list = new ArrayList<>();
       if (matcher.find()) {
           list.add(matcher.group());
       }

       int n = list.size();
       if (n > 0)
           return list.get(n-1);

        return "";
    }

    //处罚对象
    static String GetChuFaDuiXiang(List<String> list,Document html) {

        //处罚对象出现最前面几个字符
        int count_len = 0;
        List<String> new_list = new ArrayList<>();
        for (String s : list){
            new_list.add(s);
            count_len += s.length();
            if (count_len > 100)
                break;
        }
        list = new_list;

        Pattern pattern = Pattern.compile("当事人姓名：?:?.*");
        for (String str : list){
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()){
                String name = matcher.group();
                return name.substring(6);
            }

        }

        pattern = Pattern.compile("(受处罚单位：?:?.*)|(受处罚机构：.*)|(被处罚单位：.*)|(主要负责人：.*)");
        for (String str : list) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                if (name.contains("地址")){
                    int index = name.indexOf("地址");
                    name = name.substring(0, index);
                }
                return name.substring(6);
            }
        }

        pattern = Pattern.compile("(受处罚人：?:?.*)|(单位名称：.*)|(个人姓名：.*)|(受罚单位：.*)|(被处罚人：.*)");
        for (String str : list) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                if (name.contains("经查")){
                    int index = name.indexOf("经查");
                    name = name.substring(0, index);
                }else if (name.contains("地址")){
                    int index = name.indexOf("地址");
                    name = name.substring(0, index);
                }

                return name.substring(5);
            }
        }

        pattern = Pattern.compile("(被处罚单位名称:.*)|(被处罚单位名称：.*)|(被处罚个人姓名：.*)");
        for (String str : list) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                return name.substring(8);
            }
        }

        pattern = Pattern.compile("被处罚人姓名：.*");
        for (String str : list) {
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()) {
                String name = matcher.group();
                return name.substring(7);
            }
        }

        pattern = Pattern.compile("(当事人：?:?.*)|(受罚人：.*)");
        for (String str : list){
            Matcher matcher = pattern.matcher(str);
            if (matcher.find()){
                String name = matcher.group();
                if (name.length() > 30 || name.contains("具的说明等证据证明"))
                    continue;
                return name.substring(4);
            }

        }



    return "";
    }
}
